﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFUICopy
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Barcodes bb = new Barcodes();
            bb.BarcodeType = Barcodes.BarcodeEnum.Code39;
            bb.Data = "10001";
            bb.CheckDigit = Barcodes.YesNoEnum.Yes;
            bb.encode();

            int thinWidth;
            int thickWidth;

            thinWidth = 2;
            thickWidth = 3 * thinWidth;

            string outputString = bb.EncodedData;
            string humanText = bb.HumanText;

            int len = outputString.Length;
            int currentPos = 50;
            int currentTop = 340;
            int currentColor = 0;
            for (int i = 0; i < len; i++)
            {
                Rectangle rect = new Rectangle();
                rect.Height = 80;
                if (currentColor == 0)
                {
                    currentColor = 1;
                    rect.Fill = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    currentColor = 0;
                    rect.Fill = new SolidColorBrush(Colors.White);
                }
                Canvas.SetLeft(rect, currentPos);
                Canvas.SetTop(rect, currentTop);

                if (outputString[i] == 't')
                {
                    rect.Width = thinWidth;
                    currentPos += thinWidth;
                }
                else if (outputString[i] == 'w')
                {
                    rect.Width = thickWidth;
                    currentPos += thickWidth;
                }
                mainCanvas.Children.Add(rect);
            }

            TextBlock tb = new TextBlock();
            tb.Text = humanText;
            tb.FontSize = 25;
            tb.FontFamily = new FontFamily("Consolas");
            Rect rx = new Rect(0, 0, 0, 0);
            tb.Arrange(rx);
            Canvas.SetLeft(tb, 120);
            Canvas.SetTop(tb, currentTop + 80);
            mainCanvas.Children.Add(tb);
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void Close_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            CopyUIElementToClipboard(this.empCard);
        }

        public static void CopyUIElementToClipboard(FrameworkElement ui)
        {
            double width = ui.ActualWidth;
            double height = ui.ActualHeight;
            RenderTargetBitmap bmp = new RenderTargetBitmap((int)Math.Round(width),
                (int)Math.Round(height), 96, 96, PixelFormats.Default);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(ui);
                dc.DrawRectangle(vb, null,
                      new Rect(new Point(), new Size(width, height)));
            }
            bmp.Render(dv);
            Clipboard.SetImage(bmp);
        }
    }
}
